home *** CD-ROM | disk | FTP | other *** search
- /*
- * File: DZSound.c
- * Author: Dan Venolia
- *
- * Contents: Handles some of the sound stuff.
- *
- * Copyright © 1996 Apple Computer, Inc.
- */
-
- #include <assert.h>
- #include <math.h>
-
- #include <Controls.h>
- #include <Dialogs.h>
- #include <Sound.h>
-
- #include "SoundSprocket.h"
-
- #include "DZResource.h"
- #include "DZSound.h"
-
- static ListenerReference gSoundListener = NULL;
- static UserItemUPP gSoundBoxUserItemProc = NULL;
- static UserItemUPP gSoundOKUserItemProc = NULL;
-
-
- static pascal void Sound_BoxUserItem(
- DialogPtr inDialog,
- short inItem);
-
- static pascal void Sound_OKUserItem(
- DialogPtr inDialog,
- short inItem);
-
-
- /* =============================================================================
- * Sound_Init (external)
- *
- * Initializes the sound stuff.
- * ========================================================================== */
- void Sound_Init(
- void)
- {
- NewListener(&gSoundListener);
- assert(gSoundListener != NULL);
-
- SetListenerMetersPerUnit(gSoundListener, 5.0);
-
- gSoundBoxUserItemProc = NewUserItemProc(Sound_BoxUserItem);
- assert(gSoundBoxUserItemProc != NULL);
-
- gSoundOKUserItemProc = NewUserItemProc(Sound_OKUserItem);
- assert(gSoundOKUserItemProc != NULL);
- }
-
-
- /* =============================================================================
- * Sound_Exit (external)
- *
- * Prepares for exit.
- * ========================================================================== */
- void Sound_Exit(
- void)
- {
- if (gSoundListener != NULL)
- {
- DisposeListener(gSoundListener);
- gSoundListener = NULL;
- }
-
- if (gSoundBoxUserItemProc != NULL)
- {
- DisposeRoutineDescriptor(gSoundBoxUserItemProc);
- gSoundBoxUserItemProc = NULL;
- }
-
- if (gSoundOKUserItemProc != NULL)
- {
- DisposeRoutineDescriptor(gSoundOKUserItemProc);
- gSoundOKUserItemProc = NULL;
- }
- }
-
-
- /* =============================================================================
- * Sound_GetListener (external)
- *
- * Returns the game listener object, which is the "head" position for 3D sound.
- * ========================================================================== */
- ListenerReference Sound_GetListener(
- void)
- {
- assert(gSoundListener != NULL);
- return gSoundListener;
- }
-
-
- /* =============================================================================
- * Sound_Configure (external)
- *
- * Presents the modal dialog to configure the 3D sound stuff. Because the
- * state of the 3D sound speaker kind and angle is global to all localized
- * sound channels, we make a fresh channel for accessing the parameters.
- * ========================================================================== */
- void Sound_Configure(
- void)
- {
- //• TODO: Command-period should cancel
-
- DialogPtr dialog;
- short itemType;
- Handle itemHandle;
- Rect itemBounds;
- ControlHandle stereoControl;
- ControlHandle monoControl;
- ControlHandle headphonesControl;
- ControlHandle angleControl;
- SndChannelPtr sndChannel;
- Snd3DSetup snd3DInitialSetup;
- Snd3DSetup snd3DSetup;
- SoundComponentLink link;
- Boolean changed;
- Boolean ok;
- short item;
-
- // Grab the dialog
- dialog = GetNewDialog(kDlogID_Config3DSound, NULL, (WindowPtr) -1);
- assert(dialog != NULL);
-
- // Grab the control handles
- GetDialogItem(dialog, kConfig3DSoundItem_Stereo, &itemType, &itemHandle, &itemBounds);
- stereoControl = (ControlHandle) itemHandle;
-
- GetDialogItem(dialog, kConfig3DSoundItem_Mono, &itemType, &itemHandle, &itemBounds);
- monoControl = (ControlHandle) itemHandle;
-
- GetDialogItem(dialog, kConfig3DSoundItem_Headphones, &itemType, &itemHandle, &itemBounds);
- headphonesControl = (ControlHandle) itemHandle;
-
- GetDialogItem(dialog, kConfig3DSoundItem_Angle, &itemType, &itemHandle, &itemBounds);
- angleControl = (ControlHandle) itemHandle;
-
- // Do the user items
- GetDialogItem(dialog, kConfig3DSoundItem_SetupBox, &itemType, &itemHandle, &itemBounds);
- SetDialogItem(dialog, kConfig3DSoundItem_SetupBox, itemType, (Handle) gSoundBoxUserItemProc, &itemBounds);
-
- GetDialogItem(dialog, kConfig3DSoundItem_AngleBox, &itemType, &itemHandle, &itemBounds);
- SetDialogItem(dialog, kConfig3DSoundItem_AngleBox, itemType, (Handle) gSoundBoxUserItemProc, &itemBounds);
-
- GetDialogItem(dialog, kConfig3DSoundItem_OKHilite, &itemType, &itemHandle, &itemBounds);
- SetDialogItem(dialog, kConfig3DSoundItem_OKHilite, itemType, (Handle) gSoundOKUserItemProc, &itemBounds);
-
- // Set up a sound channel to control the speaker kind and angle
- sndChannel = NULL;
- SndNewChannel(&sndChannel, sampledSynth, initMono, NULL);
- assert(sndChannel != NULL);
-
- // Install the 3D sound filters
- link.description.componentType = kSoundEffectsType;
- link.description.componentSubType = kSnd3DSubType;
- link.description.componentManufacturer = 'appl';
- link.description.componentFlags = 0;
- link.description.componentFlagsMask = 0;
- link.mixerID = nil;
- link.linkID = nil;
-
- SndSetInfo(sndChannel, siPreMixerSoundComponent, &link);
-
- // Get the initial state of the setup
- SndGetInfo(sndChannel, si3DSetup, &snd3DSetup);
- snd3DInitialSetup = snd3DSetup;
-
- // Process events
- changed = true;
- ok = true;
- do
- {
- // Update the dialog
- if (changed)
- {
- SetControlValue(stereoControl, snd3DSetup.speakerKind == kSpeakerKindStereo);
- SetControlValue(monoControl, snd3DSetup.speakerKind == kSpeakerKindMono);
- SetControlValue(headphonesControl, snd3DSetup.speakerKind == kSpeakerKindHeadphones);
- SetControlValue(angleControl, snd3DSetup.speakerAngle * 180.0 / _PI);
-
- HiliteControl(angleControl, (snd3DSetup.speakerKind == kSpeakerKindStereo) ? 0 : 255);
-
- changed = false;
- }
-
- // Grab the next dialog event
- ModalDialog(NULL, &item);
-
- // Process the dialog item
- switch (item)
- {
- case kConfig3DSoundItem_OK:
- ok = false;
- break;
-
- case kConfig3DSoundItem_Cancel:
- snd3DSetup = snd3DInitialSetup;
- changed = true;
- ok = false;
- break;
-
- case kConfig3DSoundItem_Stereo:
- snd3DSetup.speakerKind = kSpeakerKindStereo;
- changed = true;
- break;
-
- case kConfig3DSoundItem_Mono:
- snd3DSetup.speakerKind = kSpeakerKindMono;
- changed = true;
- break;
-
- case kConfig3DSoundItem_Headphones:
- snd3DSetup.speakerKind = kSpeakerKindHeadphones;
- changed = true;
- break;
-
- case kConfig3DSoundItem_Angle:
- snd3DSetup.speakerAngle = GetControlValue(angleControl) * _PI / 180.0;
- changed = true;
- break;
-
- default:
- assert(0);
- }
-
- // Update the sound channel
- if (changed)
- {
- SndSetInfo(sndChannel, si3DSetup, &snd3DSetup);
- }
- }
- while (ok);
-
- // Get rid of our channel
- SndDisposeChannel(sndChannel, true);
- sndChannel = NULL;
-
- // Take down the dialog
- DisposDialog(dialog);
- }
-
-
- /* =============================================================================
- * Sound_BoxUserItem (internal)
- *
- * Draws the user item by framing it with a gray box.
- * ========================================================================== */
- pascal void Sound_BoxUserItem(
- DialogPtr inDialog,
- short inItem)
- {
- short itemType;
- Handle itemHandle;
- Rect itemBounds;
- RGBColor color;
-
- GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
-
- color.red =
- color.green =
- color.blue = 0x7777;
-
- RGBForeColor(&color);
-
- FrameRect(&itemBounds);
-
- color.red =
- color.green =
- color.blue = 0x0000;
-
- RGBForeColor(&color);
- }
-
-
- /* =============================================================================
- * Sound_OKUserItem (internal)
- *
- * Draws the user item used for framing the OK button. The user item should
- * be four pixels larger than the OK button in each direction.
- * ========================================================================== */
- pascal void Sound_OKUserItem(
- DialogPtr inDialog,
- short inItem)
- {
- short itemType;
- Handle itemHandle;
- Rect itemBounds;
-
- GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
-
- PenSize(3, 3);
- FrameRoundRect(&itemBounds, 16, 16);
- PenSize(1, 1);
- }
-
-
-